<?
$action = new Action();
class Action
{
	function Action()
	{
		global $gAct,$gOp,$gSmarty;
		$gSmarty->assign("act",$gAct);
		switch($gOp)
		{
 			case "saveRef":
			    $this->saveRef();
			    break;
			default:
				$this->index();
				break;
		}
	}
	function saveRef()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		$f = $DB->getDBFields($INCOME);
		
		$f['isPost'] = 1;
		$f['uid']= $_SESSION['adminUser']['id'];
		$f['uname']= $_SESSION['adminUser']['name'];
		if($INCOME['isPost']==-1)
		{
			$f['isPost'] = 0;
		}
		$r = $DB->db_query_first("select * from res_ref where id=".$INCOME['id']);
		if($r)
		{
			$f['edtTime'] = $gCurTime;
			$DB->db_form_update("res_ref",$f,"id=".$INCOME['id']);
		}
        else
		{
			$f['addTime'] = $gCurTime;
			$f['id'] = $INCOME['id'];
			$DB->db_form_insert("res_ref",$f);
		}
		echo '<script>parent.saveResult(1);</script>';
		
	}
	function ajaxType()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson;
		$sql = "SELECT * FROM type where parId=".$INCOME['id'];
		$DB->db_query($sql);
		$games = array();
		while($r=$DB->db_fetch_row())
		{
			$games[] = array("id"=>$r['id'],"name"=>$r['name']);
		}
		echo $GJson->encode($games);
	}
	function detail()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime;
		$rf = $DB->db_query_first("select * from res_file where id=".$INCOME['id']);
		$gSmarty->assign("f",$rf);
		$gSmarty->display("upload/".__FUNCTION__.".tpl");
	}
    function saveFile()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime;
		 
		//$INCOME['path'] = str_replace("\\","/",urldecode($INCOME['path']));
		//echo urldecode($INCOME['path']);
		$fileInfo = pathinfo($INCOME['path']);
		//var_dump($fileInfo);
		$f['fid'] =  uniqid("file_");
		$newPath = $fileInfo['dirname']."/".$f['fid'].".".$fileInfo['extension'];
		rename($INCOME['path'],$newPath);
		$f['name'] = $INCOME['name'];
		$f['ext'] =  strtolower($fileInfo['extension']);
		if($f['ext']=="jpeg")
		{
			$f['ext'] = str_replace("jpeg","jpg",$f['ext']);
		}
		$f['path']= $newPath;
		if($f['ext']=="jpg" || $f['ext']=="png" || $f['ext']=="gif")
		{
			$minPath = str_replace(".","s.",$f['path']);
			$gWeb->resizeImage($f['path'],$minPath,150,150);
			$f['path']= $minPath;
		}
		$f['uid']= $_SESSION['adminUser']['id'];
		$f['uname']= $_SESSION['adminUser']['name'];
		$f['size'] = filesize($newPath);
		$f['addTime'] =$gCurTime;
		$DB->db_form_insert("res_file",$f);
	}
	function index()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION;
 
		$sql = "SELECT * FROM res_ref order by id";
		$DB->db_query($sql);
		$data = null;
		$idx = 0+($page-1)*$iPerPage;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
            if(empty($r['pic']))
			{
				$r['pic'] = 'res/de.png';
			}
		    $data[] = $r;	
		}
		if(empty($data))
		{
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
		}
		$gSmarty->assign("data",$data);
		$gSmarty->display(basename(__FILE__,'.php')."/".__FUNCTION__.".tpl");
	}
}
?>